﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using QLKN.DataAccess;
using System.Text.RegularExpressions;
namespace QLKN.GUI
{
    public partial class frmQLKhachHang : Form
    {
        public frmQLKhachHang()
        {
            InitializeComponent();
        }
        SqlConnection conn;
        SqlCommand cmd;
        SqlDataAdapter da;
        DataSet ds;
        public DataTable LayGioiTinh()
        {
            conn = Data.getConnection();
            conn.Open();
            string sql = @"SELECT DISTINCT kGioitinh, TenGT=CASE "
                                            + "kGioitinh WHEN 'true' THEN N'Nam'"
                                            + "WHEN 'false' THEN N'Nữ' END FROM Khachhang";
            DataTable dt = Data.getDataTable(sql, conn);
            return dt;
        }

        private void frmQLKhachHang_Load(object sender, EventArgs e)
        {
            cmbGioitinh.DataSource = LayGioiTinh();
            cmbGioitinh.DisplayMember = "TenGT";
            cmbGioitinh.ValueMember = "kGioitinh";

            colGioitinh.DataSource = LayGioiTinh();
            colGioitinh.DisplayMember = "TenGT";
            colGioitinh.ValueMember = "kGioitinh";

            conn = Data.getConnection();
            cmd = new SqlCommand("SELECT * FROM Khachhang", conn);
            conn.Open();
            da = new SqlDataAdapter(cmd);
            ds = new DataSet();
            da.Fill(ds, "khachhang");
            SqlCommandBuilder cmb = new SqlCommandBuilder(da);
            dgv_QLKH.DataSource = ds.Tables["khachhang"];
            conn.Close();
            btnLuu.Enabled = false;
            btnKhongluu.Enabled = false;
        }

        private void txt_TenKH_KeyUp(object sender, KeyEventArgs e)
        {
            string sql = "SELECT * FROM Khachhang WHERE kTenkhachhang LIKE N'%" + txt_TenKH.Text + "%'";
            conn = Data.getConnection();
            cmd = new SqlCommand(sql, conn);
            conn.Open();
            da = new SqlDataAdapter(cmd);
            ds = new DataSet();
            da.Fill(ds, "khachhang");
            SqlCommandBuilder cmb = new SqlCommandBuilder(da);
            dgv_QLKH.DataSource = ds.Tables["khachhang"];
            conn.Close();
        }

        private void dgv_QLKH_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                int dong = e.RowIndex;
                txtTenkhachhang.Text = dgv_QLKH.Rows[dong].Cells["colTenkhachhang"].Value.ToString();
                cmbGioitinh.SelectedValue = dgv_QLKH.Rows[dong].Cells["colGioitinh"].Value.ToString();
                dTP_Ngaysinh.Text = dgv_QLKH.Rows[dong].Cells["colNgaysinh"].Value.ToString();
                txtDiachi.Text = dgv_QLKH.Rows[dong].Cells["ColDiachi"].Value.ToString();
                txtSDT.Text = dgv_QLKH.Rows[dong].Cells["colSodienthoai"].Value.ToString();
                txtEmail.Text = dgv_QLKH.Rows[dong].Cells["colEmail"].Value.ToString();

                if (dgv_QLKH.Rows[dong].Cells["colTrangthai"].Value.ToString().Equals("True"))
                {
                    rdHoatdong.Checked = true;
                    rdKhongHD.Checked = false;
                }
                else
                {
                    rdHoatdong.Checked = false;
                    rdKhongHD.Checked = true;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        string match = @"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*";
        

        private void btnThem_Click(object sender, EventArgs e)
        {
            btnThem.Enabled = false;
            btnXoa.Enabled = false;
            btnCapnhat.Enabled = false;
            btnThoat.Enabled = false;

            btnKhongluu.Enabled = true;
            btnLuu.Enabled = true;

            cmbGioitinh.SelectedValue = true;
            txtTenkhachhang.Text = "";
            cmbGioitinh.Text = "";
            dTP_Ngaysinh.Text = "";
            txtDiachi.Text = "";
            txtSDT.Text = "";
            txtEmail.Text = "";
            rdHoatdong.Checked = true;
            txtTenkhachhang.Focus();
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
                    Regex reg = new Regex(match);
                    if (reg.IsMatch(this.txtEmail.Text))
                        try
                        {
                            if (MessageBox.Show("Bạn có muốn thêm mới không?", "THÔNG BÁO", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                            {
                                DataTable dt = new DataTable();
                                dt = ds.Tables["khachhang"];
                                DataRow row = dt.NewRow();
                                row["kTenkhachhang"] = txtTenkhachhang.Text;
                                row["kGioitinh"] = cmbGioitinh.SelectedValue;
                                row["kNgaysinh"] = dTP_Ngaysinh.Value;
                                row["kDiachi"] = txtDiachi.Text;
                                row["kSodienthoai"] = txtSDT.Text;
                                row["kEmail"] = txtEmail.Text;
                                row["kTrangthai"] = rdHoatdong.Checked; ;
                                dt.Rows.Add(row);
                                da.Update(ds, "khachhang");
                                frmQLKhachHang_Load(sender, e);
                            }
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                        }
                    else
                    {
                        MessageBox.Show("Email không hợp lệ !");
                        txtEmail.SelectAll();
                        txtEmail.Focus();
                    }
        }

        private void btnCapnhat_Click(object sender, EventArgs e)
        {
            try
            {
                if (MessageBox.Show("Bạn có cập nhật thông tin khách hàng không?", "THÔNG BÁO", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {

                    DataTable dt = new DataTable();
                    dt = ds.Tables["khachhang"];
                    int i = dgv_QLKH.CurrentRow.Index;
                    dt.Rows[i]["kTenkhachhang"] = txtTenkhachhang.Text;
                    dt.Rows[i]["kGioitinh"] = cmbGioitinh.SelectedValue;
                    dt.Rows[i]["kNgaysinh"] = dTP_Ngaysinh.Value;
                    dt.Rows[i]["kDiachi"] = txtDiachi.Text;
                    dt.Rows[i]["kSodienthoai"] = txtSDT.Text;
                    dt.Rows[i]["kEmail"] = txtEmail.Text;
                    dt.Rows[i]["kTrangthai"] = rdHoatdong.Checked;

                    da.Update(ds, "khachhang");
                    frmQLKhachHang_Load(sender, e);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            int i = dgv_QLKH.CurrentRow.Index;
            try
            {
                if (MessageBox.Show("Bạn có muốn xoá khách hàng này không?", "THÔNG BÁO", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    ds.Tables["khachhang"].Rows[i].Delete();
                    da.Update(ds, "khachhang");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void txtSDT_MouseUp(object sender, MouseEventArgs e)
        {
            
        }

        private void txtSDT_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
                MessageBox.Show("Số điện thoại phải nhập số!");
            }
            
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnKhongluu_Click(object sender, EventArgs e)
        {
            btnLuu.Enabled = false;
            btnThem.Enabled = true;
            btnCapnhat.Enabled = true;
            btnXoa.Enabled = true;
            btnThoat.Enabled = true;
            btnKhongluu.Enabled = false;
        }
        
    }
}
